﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FinalTestMachine.Motion.Product
{
    /// <summary>
    /// 产品每项测试数据
    /// </summary>
    public class ProductTestStationTestItem
    {
        public ProductTestStationTestItem() { }

        public ProductTestStationTestItem(ProductTestStationTestItem item)
        {
            this.name = item.name;
            this.result = item.result;
            this.value = item.value;
        }

        /// <summary>
        /// 测的是什么【解释】
        /// </summary>
        public string message { get; set; } = "";
        /// <summary>
        /// 测试项的名字【尽量用字母】
        /// </summary>
        public string name { get; set; } = "";
        /// <summary>
        /// 0失败 1成功
        /// </summary>
        public string result { get; set; } = "";

        /// <summary>
        /// 测的值
        /// </summary>
        public string value { get; set; } = "";

        /// <summary>
        /// 下限值
        /// </summary>
        public string lowLimit { get; set; } = "";

        /// <summary>
        /// 上限值
        /// </summary>
        public string upLimit { get; set; } = "";

        /// <summary>
        /// 标准值
        /// </summary>
        public string standard { get; set; } = "";

        public string GetStdString()
        {
            return $"[message={this.message}," +
                $"name={this.name}," +
                $"result={this.result}," +
                $"value={this.value}," +
                $"lowLimit={this.lowLimit}," +
                $"upLimit={this.upLimit}," +
                $"standard={this.standard}]";

        }

        public static List<ProductTestStationTestItem> ChangeMsg(string msg)
        {
            List<ProductTestStationTestItem> testItemsList = new List<ProductTestStationTestItem>();
            if (msg == null) return testItemsList;

            Type type = typeof(ProductTestStationTestItem);
            PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            void Calid(string strInput)
            {
                string tempMsg = strInput;
                int index_1 = tempMsg.IndexOf('[');
                int index_2 = tempMsg.IndexOf(']');
                if (index_1 == -1)
                {
                    return;
                }
                else
                {
                    if (index_2 == -1)
                    {
                        return;
                    }
                    else
                    {
                        if ((index_2 - index_1) != 1 && (index_2 - index_1) > 0)
                        {
                            string str = tempMsg.Substring(index_1, index_2 - index_1 + 1);
                            
                            str = str.TrimStart('[');
                            str = str.TrimEnd(']');
                            if (!string.IsNullOrEmpty(str))
                            {
                                string[] strValue = str.Split(',');
                                ProductTestStationTestItem testItemMsg = new ProductTestStationTestItem();
                                bool IsCanAdd = false;

                                foreach (var item2 in strValue)
                                {
                                    if (item2.Contains('='))
                                    {
                                        foreach (var item in propertyInfos)
                                        {
                                            if (item2.Split('=')[0].ToLower() == item.Name.ToLower())
                                            {
                                                object obValue = Convert.ChangeType(item2.Split('=')[1], item.PropertyType);
                                                item.SetValue(testItemMsg, obValue);
                                                if (!IsCanAdd) IsCanAdd = true;
                                            }
                                        }
                                    }
                                }
                                if (IsCanAdd)
                                {
                                    testItemsList.Add(testItemMsg);
                                }
                            }
                        }
                        Calid(strInput.Substring(index_2 + 1));
                    }
                }
            }
            try
            {
                Calid(msg);
            }
            catch
            {


            }



            return testItemsList;
        }



        public static List<T> ConvertByPropertyName<T>(List<ProductTestStationTestItem> testItemMsgs) where T : new()
        {
            List<T> Temp = new List<T>();
            if (testItemMsgs == null || testItemMsgs.Count == 0) return Temp;

            Type src_type = typeof(ProductTestStationTestItem);
            Type dst_Type = typeof(T);

            PropertyInfo[] propertyInfos_src = src_type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            PropertyInfo[] propertyInfos_dst = dst_Type.GetProperties(BindingFlags.Public | BindingFlags.Instance);



            bool IsCanSet = false;
            Action<T, ProductTestStationTestItem> SetProEvent = (d, s) => { };

            foreach (var item_src in propertyInfos_src)
            {
                foreach (var item_dst in propertyInfos_dst)
                {
                    if (item_src.Name.ToLower() == item_dst.Name.ToLower())
                    {
                        if (item_src.PropertyType == item_dst.PropertyType)
                        {
                            IsCanSet = true;
                            SetProEvent += (d, s) =>
                            {
                                item_dst.SetValue(d, item_src.GetValue(s));
                            };

                            break;
                        }
                    }

                }

            }
            if (!IsCanSet) return Temp;

            foreach (var item in testItemMsgs)
            {
                T t = new T();
                SetProEvent(t, item);
                Temp.Add(t);
            }




            return Temp;

        }
    }
}
